/* 
Note: 
For some job characteristic there is both the main and a *_rev version in the dataset with different values. For the time being I kept both and took an average.
*/

foreach X in 93 08{
	* get the physically demanding characteristics
	use "$code/onet_var_desc.dta", clear
	tab physical2check
	keep if physical2check==1
	drop if inlist(ONETvar,"4A3b1","4A3b2","4A3b6","4C2a1a","4C2a1d","4C2a1f","4C2a3","4C2a3_rev","4C2d1a")
	keep ONETvar
	tempfile temp
	save `temp'
	* create the broad version of the physically demanding index
	use "$out_ONET/feor`X'.dta", clear
	reshape long t_, i(feor`X') j(ONETvar) string
	merge m:1 ONETvar using `temp'
	egen physical_b=mean( t_) if _merge==3, by(feor`X')
	egen physical_broad=max(physical_b), by(feor`X')
	sort feor`X'
	drop physical_b _merge
	* create a narrow version of the physically demanding index
	preserve
	use `temp', clear
	keep if substr(ONETvar,1,2)=="1A" | inlist(substr(ONETvar,1,4),"4A3a","4C2b","4C2c","4C2d","4C2e")
	save `temp', replace
	use "$code/onet_var_desc.dta", clear
	keep if physical2check==1
	drop if inlist(ONETvar,"4A3b1","4A3b2","4A3b6","4C2a1a","4C2a1d","4C2a1f","4C2a3","4C2d1a")
	keep if substr(ONETvar,1,2)=="1A" | inlist(substr(ONETvar,1,4),"4A3a","4C2b","4C2c","4C2d","4C2e")
	restore
	merge m:1 ONETvar using `temp'
	egen physical_n=mean( t_) if _merge==3, by(feor`X')
	egen physical_narrow=max(physical_n), by(feor`X')
	drop physical_n _merge
	
	* get the remote work characteristics
	preserve
	use "$code/onet_var_desc.dta", clear
	keep if remote2check==1
	drop if inlist(ONETvar,"4C1a2c","4C1a2j","4C1c1","4C1c2","4C1d1","4C1d2")
	* mark if the characteristic refers to "non-remote" to change the value from x to 6-x later
	gen byte neg=inlist(ONETvar,"2B3d","2B3g","2B3h","2B3j","2B3k","2B3l","4C1a2l","4C1a21_rev") | inlist(ONETvar,"4C1a4","4C1b1e","4C1b1f","4C1b1g","4C1d3")
	tab neg
	keep ONETvar neg
	save `temp', replace
	restore
	merge m:1 ONETvar using `temp'
	* change the value from x to 6-x if the characteristic refers to "non-remote"
	gen t_sign=6-t_ if neg==1
	replace t_sign=t_ if t_sign==.
	* create a broad version of the remote work index
	egen remote_b=mean( t_sign) if _merge==3, by(feor`X')
	egen remote_broad=max(remote_b), by(feor`X')
	drop remote_b _merge
	* create a narrow version of the remote work index
	preserve
	use `temp', clear
	keep if inlist(ONETvar,"2B3e","4C1a2f","4C1a2h","4C1a2l","4C1a21_rev","4C1d3")
	save `temp', replace
	restore
	merge m:1 ONETvar using `temp'
	egen remote_n=mean( t_sign) if _merge==3, by(feor`X')
	egen remote_narrow=max(remote_n), by(feor`X')
	drop remote_n _merge t_sign neg
	
	* get the responsible work characteristics
	preserve
	use "$code/onet_var_desc.dta", clear
	keep if responsible2check==1
	drop if inlist(ONETvar,"1C3a","1C3b","1C3c","1C4a","1C4b","1C4c","1C5b","1C5c","1C7a") | inlist(ONETvar,"1C7b","4A2b2","4A2b3","4A4c1","4C1a2f","4C1a2h","4C1a2j","4C1a4","4C1b1e") | inlist(ONETvar,"4C1b1f","4C3b2","4C3b7","4C3c1") | inlist(substr(ONETvar,1,4),"4A4a","4C1d","4C3d") | substr(ONETvar,1,5)=="4C3b8" | substr(ONETvar,1,6)=="4C1a2l" 
	keep ONETvar
	save `temp', replace
	restore
	merge m:1 ONETvar using `temp'
	* create a broad version of the remote work index
	egen respons_b=mean( t_) if _merge==3, by(feor`X')
	egen respons_broad=max(respons_b), by(feor`X')
	drop respons_b _merge
	* create a narrow version of the remote work index
	preserve
	use `temp', clear
	keep if inlist(ONETvar,"1C1c","1C2b","2B5a","2B5b","2B5c","2B5d","4A2a1","4A2a3","4A2b1") | inlist(ONETvar,"4A2b4","4A2b6","4A4c3","4C1b1g","4C1c1","4C1c2","4C3a1","4C3a2a","4C3a2b") | substr(ONETvar,1,4)=="4A4b"
	save `temp', replace
	restore
	merge m:1 ONETvar using `temp'
	egen respons_n=mean( t_) if _merge==3, by(feor`X')
	egen respons_narrow=max(respons_n), by(feor`X')
	drop respons_n _merge 
	
	* get the working with others characteristics
	
	preserve
	use "$code/onet_var_desc.dta", clear
	keep if others2check==1
	drop if inlist(ONETvar,"4A4a1","4A4c1","4A4c3","4C1a2h","4C1a2j","4C1c1","4C1c2") | substr(ONETvar,1,4)=="4C1d"
	keep ONETvar
	save `temp', replace
	restore
	merge m:1 ONETvar using `temp'
	* create a broad version of the remote work index
	egen others_b=mean( t_) if _merge==3, by(feor`X')
	egen others_broad=max(others_b), by(feor`X')
	drop others_b _merge
	* create a narrow version of the remote work index
	preserve
	use `temp', clear
	keep if inlist(ONETvar,"4A4a4","4A4a4_rev","4C1b1e") | substr(ONETvar,1,4)=="4A4b"
	save `temp', replace
	restore
	merge m:1 ONETvar using `temp'
	egen others_n=mean( t_) if _merge==3, by(feor`X')
	egen others_narrow=max(others_n), by(feor`X')
	drop others_n _merge 
	
	* get the stressful work characteristics
	
	preserve
	use "$code/onet_var_desc.dta", clear
	keep if stress2check==1
	drop if inlist(ONETvar,"1C7a","1C7b","4C2d1a","4C2d1b","4C2d1d","4C2d1e","4C2d1g","4C2d1h","4C2d1i") | inlist(ONETvar,"4C3a4","4C3a2","4C3b7") | substr(ONETvar,1,4)=="4C2a" | substr(ONETvar,1,5)=="4C3b8" 	
	keep ONETvar
	save `temp', replace
	restore
	merge m:1 ONETvar using `temp'
	* create a broad version of the remote work index
	egen stress_b=mean( t_) if _merge==3, by(feor`X')
	egen stress_broad=max(stress_b), by(feor`X')
	drop stress_b _merge
	* create a narrow version of the remote work index
	preserve
	use `temp', clear
	keep if inlist(ONETvar,"1C1a","1C4b","4C2e1e","4C3a1","4C3c1","4C3d1") 
	save `temp', replace
	restore
	merge m:1 ONETvar using `temp'
	egen stress_n=mean( t_) if _merge==3, by(feor`X')
	egen stress_narrow=max(stress_n), by(feor`X')
	drop stress_n _merge 
	
	* standardization
	
	drop ONETvar t_
	duplicates drop
	foreach Y in physical remote respons others stress{
		foreach Z in broad narrow{
			egen std_`Y'_`Z'=std(`Y'_`Z')
			rename `Y'_`Z' raw_`Y'_`Z'
		}
	}
	sum	
	* save
	save "$out_ONET/onet_indices_feor`X'.dta"
}	
	
	
/*
physical
--------

the higher the more physically demanding

narrow version: 1A, 4A3a, 4C2b, 4C2c, 4C2d, 4C2e

dropped:
4A3b1- working with a computer
4A3b2 - drafting, laying out and specifying technial devices
4Ab36 - documenting and recording information
4C2a1a - working indoors in a controlled environment
4C2a1d - working outdoors under cover
4C2a1f - working in a closed vehicle
4C2a3 - close physical proximity to others
4C2d1a - sitting

remote
------

the higher the more remote

narrow version: 2B3e, 4C1a2f, 4C1a2h, 4C1a2l, 4C1d3

negative:
2B3d - equipment installation
2B3g - monitoring the operation of a machine
2B3h - equilment controlling
2B3j - equipment maintenance
2B3k - troubleshooting
2B3l - repairing machines
4C1a2l - face-to-face
4C1a4 - any contact with others (face-2-face or phone)
4C1b1e - work with others
4C1b1f - work with customers
4C1b1g - lead a team
4C1d3 - deal with physical aggression

dropped:
4C1a2c - public speaking
4C1a2j - written letters
4C1c1 - responsibility for safety
4C1c2 - responsibility for others' work
4C1d1 - conflict situation
4C1d2 - unpleasent people

responsible
-----------

the higher the more responsibility

narrow version: 1C1c, 1C2b, 2B5a-d, 4A2a1, 4A2a3, 4A2b1, 4A2b4, 4A2b6, 4A4b*, 4A4c3, 4C1b1g, 4C1c1, 4C1c2, 4C3a1, 4C3a2a, 4C3a2b

dropped:
1C3a - pleasant with others
1C3b - sensitive to others' needs
1C3c - working with others
1C4a - controlling emotions
1C4b - accepting criticism
1C4c - open to change
1C5b - careful about detail
1C5c - honest
1C7a - creativity
1C7b - analyzing information
4A2b2 - thinking creatively
4A2b3 - keeping up to date
4A4a* - translating info, providing info, external communication, developing work relationships, providing assistance, convincing to buy, handling complaints, performing for people
4A4c1 - daily administration
4C1a2f - using the phone
4C1a2h - using e-mail
4C1a2j - using letters
4C1a2l* - face-to-face
4C1a4 - contact with others
4C1b1e - work with others
4C1b1f - work with customers
4C1d* - conflict, unpleasant people, physical aggression
4C3b2 - automation
4C3b7 - repetitive
4C3b8* - structured work
4C3c1 - competition
4C3d* - time pressure, pace of equipment

working with others
-------------------

the higher the more work with others

narrow version: 4A4a4, 4A4a4_rev, 4A4b*, 4C1b1e

dropped:
4A4a1 - translating info
4A4c1 - daily administration
4A4c3 - controlling resources
4C1a2h - using e-mail
4C1a2j - using letters
4C1c1 - responsibility for health
4C1c2 - responsibility for outcomes
4C1d* - conflict, unpleasant people, physical aggression

stressful
---------

the higher the more stress

narrow version: 1C1a, 1C4b, 4C2e1e, 4C3a1, 4C3c1, 4C3d1

dropped:
1C7a - own solutions
1C7b - analyzing information
4C2a* - type of working location, being close to people
4C2d1a - sitting
4C2d1b - standing
4C2d1d - walking, running
4C2d1e - kneeling, crawling
4C2d1g - using hands
4C2d1h - bending
4C2d1i - repetitive
4C3a4 - freedom to make decision
4C3a2 - automation
4C3b7 - repetitive
4C3b8* - structured work



*/
